// pages/index/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    images_list : [
      {
        "id": 1,
        "image_url": "../../images/swiper/1.jpg"
      },
      {
        "id": 2,
        "image_url": "../../images/swiper/2.jpg"
      },
      {
        "id": 3,
        "image_url": "../../images/swiper/3.jpg"
      },
    ],
    /**新闻 */
    isRefresh:false,
    pageSize: 10,
    page: 0,
    scrollTop: 0,
    loading: false,
    nextLoading: false,
    scrollHeight:0,
    isShowMessageBox: false,
    messageBoxText:'',
    data_list :[],
    data_tabBar_list:[
      {
        name:'健康',
        url:'https://3g.163.com/touch/reconstruct/article/list/BDC4QSV3wangning/'
      },
      {
        name:'新闻',
        url:'https://3g.163.com/touch/reconstruct/article/list/BBM54PGAwangning/'
      }
      //……剩余的新闻分类此处省略……
    ],
    selected_tabBar:{}

      },
  onViewclick:function(){
    wx.navigateTo({
      url: '/pages/dati/dati',
    })
  },
  onView1click:function(){
    wx.switchTab({
      url: '/pages/ailife/ailife',
    })
  },
 /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    wx.hideHomeButton();  //隐藏home/返回主页按钮
  },

  /**新闻 */
  onLoad: function (options) {
    var that = this;
    that.setData({
      selected_tabBar:that.data.data_tabBar_list[0]
    })
    wx.getSystemInfo({
        success:function(res){
            that.setData({
                scrollHeight:res.windowHeight
            })
        }
    })
    wx.request({
      url: this.data.selected_tabBar.url + '0-10.html', 
      data: {},
      header: {
        'content-type': 'application/json' // 默认值
      },
      success (res) {
        that.setData({
          data_list: that.data_transform(res.data),
          loading:true
        })
      }
    })
  },//加载新闻
  data_transform:function(data){
    var tmp_data = JSON.parse(data.substring(9,data.length-1));
    for (var key in tmp_data)
        if(typeof tmp_data[key] == typeof []) 
        return tmp_data[key];
  },
  tabBarItemTap: function(e) {
    var tabBar ={};
    tabBar.name = e.detail.name;
    tabBar.url = e.detail.url;
    this.setData({
      selected_tabBar:e.detail,
      scrollTop:0
    });
    this.onRefresh();
  },
  //更新内容
  onRefresh: function(event) {
    var that = this;
    wx.request({
      url: this.data.selected_tabBar.url + '0-10.html', 
      data: {},
      header: {
        'content-type': 'application/json' // 默认值
      },
      success (res) {
        that.setData({
          data_list: that.data_transform(res.data),
          isRefresh:false,
          page:0,
          isShowMessageBox:true,
          messageBoxText: '成功为您推荐10条新内容'
        })
        // setTimeout(function(){
        //   that.setData({
        //     isShowMessageBox:false,
        //     messageBoxText:''
        //   })
        // },1000)
      }
    })
  },
  listItemTap: function(e) {
    var obj = JSON.stringify(e.currentTarget.dataset.item);
    wx.navigateTo({
      url: '/pages/news/news?obj=' + encodeURIComponent(obj), // 进行编码,
    })
  }


})